<template>
  <div class="register-container">
    <div class="login-header">
      <h1>微风医药注册</h1>
    </div>
    <el-form ref="registerForm" :model="form" label-width="100px" class="register-form">
      <el-form-item label="注册方式">
        <el-radio-group v-model="registerMethod">
          <el-radio-button :label="'phone'">手机号注册</el-radio-button>
          <el-radio-button :label="'username'">账号密码注册</el-radio-button>
        </el-radio-group>
      </el-form-item>

      <template v-if="registerMethod === 'phone'">
        <el-form-item label="手机号" prop="phone" :rules="rules.phone">
          <el-input v-model="form.phone"></el-input>
          <el-button @click="sendCode">获取验证码</el-button>
        </el-form-item>
        <el-form-item label="验证码" prop="code">
          <el-input v-model="form.code"></el-input>
        </el-form-item>
      </template>

      <template v-else>
        <el-form-item label="用户名" prop="username" :rules="rules.username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="userpassword" :rules="rules.userpassword">
          <el-input v-model="form.userpassword" type="password" show-password/>
        </el-form-item>
      </template>

      <el-form-item>
        <el-button type="primary" @click="register">注册</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import axios from "axios";
import { ElMessage } from "element-plus";

export default {
  data() {
    return {
      registerMethod: 'phone', // 默认为手机号注册
      form: {
        phone: '',
        code: '',
        username: '',
        userpassword: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
        ],
        userpassword: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    sendCode() {
      if (this.registerMethod === 'phone') {
        this.$refs.registerForm.validateField('phone', (valid) => {
          if (valid) {
            axios.get(`http://localhost:9099/user/code`, { params: { phone: this.form.phone } }).then(res => {
              if (res.data.code == 200) {
                ElMessage({
                  message: res.data.msg,
                  type: 'success',
                  duration: 2000
                });
              } else {
                ElMessage({
                  message: res.data.msg,
                  type: 'error',
                  duration: 2000
                });
              }
            });
          } else {
            this.$message.error('手机号格式不正确或为空');
          }
        });
      } else {
        this.$message.error('请先选择注册方式');
      }
    },
    register() {
      this.$refs.registerForm.validate((valid) => {
        if (valid) {
          const data = {...this.form};
          if (this.registerMethod === 'phone') {
            delete data.username;
            delete data.userpassword;
            axios.get('http://localhost:9099/user/register1', {
              params: {
                phone: this.form.phone,
                code: this.form.code,
              }
            }).then(res => {
              if (res.data.code == 200) {
                ElMessage({
                  message: res.data.msg,
                  type: 'success',
                  duration: 2000
                });
                this.$router.push('/loginView');
              } else {
                ElMessage({
                  message: res.data.msg,
                  type: 'error',
                  duration: 2000
                });
              }
            });
          } else {
            delete data.phone;
            delete data.code;
            axios.post('http://localhost:9099/user/register', data).then(res => {
              if (res.data.code == 200) {
                ElMessage({
                  message: res.data.msg,
                  type: 'success',
                  duration: 2000
                });
                this.$router.push('/loginView');
              } else {
                ElMessage({
                  message: res.data.msg,
                  type: 'error',
                  duration: 2000
                });
              }
            });
          }
        } else {
          this.$message.error('请检查输入格式');
        }
      });
    }
  }
};
</script>

<style scoped>
.register-container {
  height: 100vh;
  background-image: url('@/static/img/qq .png');
  background-size: cover;
  background-position: center;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}

.login-header h1 {
  color: #fff;
  font-size: 36px;
  font-weight: bold;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
  text-align: center;
  margin-bottom: 40px;
}

.register-form {
  max-width: 500px;
  width: 100%;
  background: #fff;
  padding: 20px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  border-radius: 10px;
}

.el-form-item {
  margin-bottom: 20px;
}

.el-button {
  background-color: #3a7ff6;
  border-color: #3a7ff6;
  color: #fff;
}

.el-button:hover {
  background-color: #337eff;
  border-color: #337eff;
}
</style>